% output = qamdemod(Fre_out,4, 'UnitAveragePower',true)';
close all
load("Signal_map_output.mat")
Ts_map_output = Ts_map_output(1,1:end);
% Signal_map_output = Signal_map_output;
output2 = qamdemod(Signal_map_output,4, 'UnitAveragePower',true,PlotConstellation=true).';
[c,lags] = xcorr(Phase_out,Signal_map_output);
[~,pos] = max(abs(c));
xx = Phase_out(1,lags(pos)+1:end);
xx = xx*exp(-1i*pi/2);

output = qamdemod(xx,4, 'UnitAveragePower',true).';
[c,lags] = xcorr(xx,Signal_map_output);
figure;stem(lags,abs(c));title("互相关函数2");
%%
% Syn_Begin=Syn_Related(Signal_map_output,Phase_out(Train_len:Train_len+1024)); % 返回同步点

load("Ts_map_output.mat");
[c,lags] = xcorr(Ts_map_output,Phase_out);
[~,pos] = max(abs(c));
figure;stem(lags,abs(c));title("互相关函数2");
Syn_Begin = lags(pos);

Syn_out=Phase_out(Train_len:end-100);
Signal_map_orgin=Signal_map_output(Syn_Begin:Syn_Begin+length(Syn_out));
Syn_out=sqrt((2^Constell-1)/1.5)*Syn_out/sqrt(mean(abs(Syn_out).^2));
Syn_out = Syn_out.*exp(-1i*pi/2);
Signal_map_orgin=sqrt((2^Constell-1)/1.5)*Signal_map_orgin/sqrt(mean(abs(Signal_map_orgin).^2));
output1 = qamdemod(Syn_out,4, 'UnitAveragePower',true)';
output2 = qamdemod(Signal_map_orgin,4, 'UnitAveragePower',true)';





%%
close all
x = [1 2 3];
y = [ 0 0  1 2 3 3 2 1];
[c,lags] = xcorr(y,x);
[~,pos] = max(abs(c));
lags(pos)
% y2 = y(1,lags(pos)+1:end);
% figure;stem(y2);
figure;stem(lags,c);title("互相关函数");
